package com.abe.jmcl.android.widget;

import android.graphics.Point;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.abe.jmcl.R;
import com.abe.jmcl.adapter.WidgetCommonAdapter;
import com.abe.jmcl.utils.ToastUtils;
import com.abeframe.core.base.MVPBaseActivity;
import com.abeframe.core.base.MVPBasePresenter;
import com.abeframe.core.libs.adapter.CommonData;
import com.abeframe.core.libs.widgets.actionsheet.ActionSheetView;
import com.abeframe.core.libs.widgets.popwindow.MapPopDialog;
import com.abeframe.core.utils.EmptyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShowWidgetActivity extends MVPBaseActivity implements AdapterView.OnItemClickListener {

    @Override
    protected MVPBasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getThemeResId() {
        return 0;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_widget_show;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected void initDatas() {
        String[] items = {"ActionSheet", "PopWindow"};
        itemWidgetList.setOnItemClickListener(this);
        List<CommonData> datas = new ArrayList<>();
        for (String name : items) {
            datas.add(new CommonData(WidgetCommonAdapter.TYPE_ACTIONSHEET, name));
        }
        WidgetCommonAdapter adapter = new WidgetCommonAdapter(this, datas);
        itemWidgetList.setAdapter(adapter);
        popDialog = new MapPopDialog(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (i) {
            case 0: {
                showActionSheet();
            }
            break;
            case 1: {
                showPopLayout();
            }
            break;
        }
    }

    private void showActionSheet() {
        String[] menus = {"点我！点我！点我！", "返回"};
        actionSheetView = new ActionSheetView(this);
        actionSheetView.setMenuItem(menus, new ActionSheetView.OnActionSheetClickListener() {
            @Override
            public void OnCancel() {

            }

            @Override
            public void OnSelect(int pos, String menu) {
                switch (pos) {
                    case 0: {
                        Toast.makeText(getApplicationContext(), "非常感谢！", Toast.LENGTH_SHORT).show();
                    }
                    break;
                    case 1: {
                        onBackPressed();
                    }
                    break;
                }
            }
        });
        actionSheetView.show();
    }

    private void showPopLayout() {
        popLayout.setVisibility(View.VISIBLE);
        popLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        popLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Point point = new Point((int) event.getX(), (int) event.getY());
                        showPopWindow("popwindow", point);
                        //起始位置：(event.getX() ,event.getY())
                        break;
                }
                return false;
            }
        });
    }

    private void showPopWindow(String message, Point point) {
        View view = LayoutInflater.from(this).inflate(R.layout.view_map_pop_normal, null);
        TextView item_name = view.findViewById(R.id.item_name);
        ImageView item_btn_close = view.findViewById(R.id.item_btn_close);
        item_name.setText(message);
        item_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.showShort("点击PopWindow");
            }
        });
        item_btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popDialog.dismiss();
            }
        });
        popDialog.setCustomView(view, 50);
        popDialog.setNoClose(true);
        popDialog.showPoint(point);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (popLayout.getVisibility() == View.VISIBLE) {
                //PopWindow
                popLayout.setVisibility(View.GONE);
                if (EmptyUtils.isNotEmpty(popDialog) && popDialog.isShowing()) {
                    popDialog.dismiss();
                }
                return false;
            } else if (EmptyUtils.isNotEmpty(actionSheetView) && actionSheetView.isShowing()) {
                //底部菜单
                actionSheetView.dismiss();
                return false;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @BindView(R.id.item_layout_pop)
    LinearLayout popLayout;
    private MapPopDialog popDialog;

    private ActionSheetView actionSheetView;

    @BindView(R.id.layout_root)
    FrameLayout layoutRoot;
    @BindView(R.id.item_widget_list)
    ListView itemWidgetList;
}